import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:open_file/open_file.dart';
import '../../helper/Helper.dart';

import '../../helper/uidata.dart';
import '../../helper/upload_helper.dart';
import '../../model/upfile.dart';

class UpFileBookPage extends StatefulWidget {
  final bool ifAppbar;

  @override
  UpFileBookPage({this.ifAppbar = false});

  @override
  UpFileBookPageState createState() => new UpFileBookPageState();
}

class UpFileBookPageState extends State<UpFileBookPage> {
  late List<UpFile> UpFileList;

  void getList() async {
    var _upList = await UploadHelper.getList();

    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      UpFileList = _upList;
    });
  }

  void getProgress() async {
    if (UploadHelper.IfInit2 == true) return;
    UploadHelper.IfInit2 = true;

    final subscription = UploadHelper.uploader.progress.listen((progress) {
      print(progress.progress);
      var a = UpFileList.firstWhere((p) => p.taskId == progress.taskId);
      //print(a.taskId);
      if (a != null) {
        setState(() {
          a.progress = (progress.progress! / 100)!;
          if (progress.progress == 100) a.done = 100;
        });
      }

      // UpFileList.firstWhere((p)=>p.taskId==progress.taskId).progress=progress.progress/100;
    });
  }

  Widget fileItem1(UpFile f1) {
    return ListTile(
      //leading: ImageWidget.UpFileImage(UpFileList[index].Name),
      title: Text(f1.tag),
      subtitle: Text(f1.baseName),
      trailing: Container(
        width: 10.0,
        height: 10.0,
        child: new CircleAvatar(
          backgroundColor: f1.done == 100 ? Colors.green : Colors.red,
          //radius: 2.0,
        ),
      ),
      onTap: () {
        OpenFile.open(f1.filePath);
      },
    );
  }

  Widget fileItem2(UpFile f1) {
    return ListTile(
      //leading: ImageWidget.UpFileImage(UpFileList[index].Name),
      title: Text(f1.tag),
      subtitle: Text(f1.baseName),
      trailing: CircularProgressIndicator(
        strokeWidth: 4.0,
        backgroundColor: Colors.red,
        value: f1.progress,
        valueColor: AlwaysStoppedAnimation<Color>(Colors.green),
      ),
      onTap: () {
        OpenFile.open(f1.filePath);
      },
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getList();
    getProgress();
  }

  @override
  Widget build(BuildContext context) {
    // getList();

    if (UpFileList == null) return new Container();
    // TODO: implement build
    return Scaffold(
        appBar: widget.ifAppbar ? AppBar(title: Text('上传列表')) : null,
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          onPressed: () async {
            File file = File(await Helper.getFilePickPath() as String);
            await UploadHelper.UpLoad(file.path, "文件上传", UploadHelper.uuid.v1(),
                UIData.NewsPaperSpaceId);
            getList();
          },
        ),
        body: new Container(
            margin: EdgeInsets.only(top: 8.0),
            child: ListView.builder(
              itemCount: UpFileList.length,
              itemBuilder: (BuildContext context, int index) {
                return new Card(
                  margin: const EdgeInsets.only(top: 3.0, bottom: 3.0),
                  shape: const RoundedRectangleBorder(
                      borderRadius: BorderRadius.zero),
                  child: UpFileList[index].done == 100
                      ? fileItem1(UpFileList[index])
                      : fileItem2(UpFileList[index]),
                );
              },
            )));
  }
}
